package internationalenergytradingplatform.controller.SupplierController;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import internationalenergytradingplatform.context.BaseContext;
import internationalenergytradingplatform.dao.PackageMapper;
import internationalenergytradingplatform.dto.PackagePageQueryDTO;
import internationalenergytradingplatform.pojo.Commodity;
import internationalenergytradingplatform.pojo.Package;
import internationalenergytradingplatform.result.PageResult;
import internationalenergytradingplatform.result.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/package")
@CrossOrigin
@Slf4j
public class PackageController {

    @Autowired
    PackageMapper packageMapper;

    /**
     *
     * @author yingfeng
     * @date 2024/3/13 14:24
    查看制定供应商的全部套餐
     */
    @GetMapping("/paginationQuery")
    public Result<PageResult> pagingToQueryAllPackage(PackagePageQueryDTO packagePageQueryDTO){
        //开始分页查询
        String companyId = BaseContext.getCurrentId();//获取公司id
        packagePageQueryDTO.setSupplierAccountr(companyId);
        PageHelper.startPage(packagePageQueryDTO.getPage(), packagePageQueryDTO.getPageSize());
        Page<Package> page = packageMapper.PackagePageQuery(packagePageQueryDTO);
        long total = page.getTotal();
        List<Package> records = page.getResult();
        PageResult pageResult = new PageResult(total, records);
        return Result.success(pageResult);
    }

    /**
     *
     * @author yingfeng
     * @date 2024/3/13 14:24
     上架/下架套餐
     */
    @PutMapping("/putUpOrDownPackage/{id}/{status}")
    public Result<?> putUpOrDownPackage(@PathVariable Integer id, @PathVariable Integer status){
        packageMapper.listingProducts(id, status);
        return Result.success();
    }
    /**
     *
     * @author yingfeng
     * @date 2024/3/12 17:29
    新增商品
     */
    @PostMapping("/newPackageAdd")
    public Result<?> newPackageAdd(@RequestBody Package package1){
        package1.setSalesStatus(1);
        String companyId = BaseContext.getCurrentId();//获取公司id
        package1.setSupplierAccountr(companyId);
        packageMapper.addPackage(package1);
        return Result.success();

    }
    /**
     * 删除商品
     */
    @DeleteMapping("/{id}")
    public Result<?> deletePackage(@PathVariable Integer id){
        packageMapper.deleteCommodity(id);
        return Result.success();
    }
    /**
     * 修改商品
     */
    @PutMapping("/modify")
    public Result<?> modifyPackage(@RequestBody Package package1){
        packageMapper.modifyPackage(package1);
        return Result.success();
    }
    /**
     * 根据id查找商品
     */
    @GetMapping("/findById/{id}")
    public Result<Package> getPackageById(@PathVariable Integer id){
        Package packageById = packageMapper.getPackageById(id);
        log.info("根据id查找商品:{}", packageById);
        return Result.success(packageById);
    }



}
